// Learn cc.Class:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/class/index.html
// Learn Attribute:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/reference/attributes/index.html
// Learn life-cycle callbacks:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/life-cycle-callbacks/index.html


cc.Class({
    extends: cc.Component,

    properties: {
        className:"cmlayer",
        cm_altas:{
            default:null,
            type:cc.SpriteAtlas
        },
        coin:{
            default:null,
            type:cc.Prefab,
        },
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad: function () {
        this.node.thisJS = this;
        this.scorenode = cc.find("scorenode",this.node);
        this.scorenode.active = false ;
        this.totalscore = cc.find("totalscore",this.scorenode);
    },
    start: function () {
        this.initUI();
        this.coins = [] ;
        this.kanlist = {} ;
    },
    initUI: function(){
          console.log("接收监听");

        EventManager.addEventObeserveF(EventConfig.startGame, handler(this,
            this.gameStart),"cmlayer");
        EventManager.addEventObeserveF(EventConfig.xiaZhu, handler(this,
            this.xiaZhuNtf),"cmlayer");
        EventManager.addEventObeserveF(EventConfig.gameoverNtf, handler(this,
            this.gameoverNtf),"cmlayer");
        EventManager.addEventObeserveF(EventConfig.clearTable, handler(this,
            this.clearTable),"cmlayer");
        EventManager.addEventObeserveF(EventConfig.kanPaiNtf, handler(this,
            this.kanPaiNtf),"cmlayer");
        EventManager.addEventObeserveF(EventConfig.UserEnterDeskNtf, handler(this,
            this.UserEnterDeskNtf),"cmlayer");
        
    },
    UserEnterDeskNtf:function(data){
        if(data.total_chips || data.total_chips>0){
             this.scorenode.active = true ;
             this.totalscore.getComponent(cc.Label).string = data.total_chips ;
        }
    },
    getMoneyBgType:function(money){
        return money + "";
    },
    /*
    message GoldFlowerUserChipNtf{
    optional int32 uin = 1;             //用户ID
    optional int32 desk_id=2;           //桌子ID
    optional int64 call_money=3;        //用户下注的钱
    optional int32 round_count=4;       //当前轮数
    optional int32 max_count=5;         //最大轮数
    optional int64 user_money=6;        //用户当前下的钱
    optional int64 desk_total_chip=7;   //桌子当前总注数
    optional int32 next_uin=8;          //下一个操作的玩家  如果为-1 表示当局已经结束 等待game_over指令
    optional int32 can_operator = 9;        // 可以执行的操作 包括 看牌 比牌 弃牌  用位数表示  11  第一位看牌  第二位比牌
    }   
    */
    xiaZhuNtf:function(data){
        var info = GameData ;
        //判断是否应该显示准备按钮
        for(var k in info.playerinfo){
            if(info.playerinfo[k].uin==data.uin){
                var user = info.playerinfo[k];
                var searId = GameData.getUserSeatP(user.seatid);
                var money =  data.call_money ;
                if(this.kanlist[data.uin] == 1){
                    money = money * 2 ;
                }
                this.flipMoney(searId,money,this.getMoneyBgType(money));
            }
         }
        this.totalscore.getComponent(cc.Label).string = "" + data.desk_total_chip ;
    },
    //结算
    gameoverNtf:function(data){
       var user = GameData.playerinfo[data.winner] ;
       var searId = GameData.getUserSeatP(user.seatid);
       var config = GameConfig.headConfig[searId];
       var orgPos = config.pos ;
       var HeadWidth = 50; 
       var HeadHeight = 50;
       for(var k in this.coins){
         var coin = this.coins[k] ;
         var move  = cc.moveTo(0.5,cc.p(orgPos.x+25+ Math.random()*HeadWidth,orgPos.y+25+Math.random()*HeadHeight));
         move.easing(cc.easeSineInOut());
         var call  = cc.callFunc(function (coin){
             coin.removeFromParent(true);
         },self,coin)
         var sq  = cc.sequence(move,call);
         coin.runAction(sq);
       }
       this.coins = [] ;
    },
    gameStart:function(data){
        this.scheduleOnce(function(){
            this.scorenode.active = true ;
            Log.logd(GameData.PlayerNum,"headlayer start->");
             var info = GameData ;
             for(var k in info.playerinfo){
                    var user = info.playerinfo[k];
                    var searId = GameData.getUserSeatP(user.seatid);
                    this.flipMoney(searId,data.play_info.round_chips,this.getMoneyBgType(data.play_info.round_chips));
             }
             this.totalscore.getComponent(cc.Label).string = ""+data.total_chips ;
        },2);
    },
    //飘钱
    flipMoney:function(seat,score,index){
        var self = this ;
        var HeadWidth = 128; 
        var HeadHeight = 178;
        //this.schedule(function(){
            var config = GameConfig.headConfig[seat];
            var orgPos = config.pos ;
            var desPos = cc.p(-80,0)
            var tempItem =  cc.instantiate(this.coin);
            var scoreLabel = cc.find("score",tempItem);
            scoreLabel.getComponent(cc.Label).string = score + "" ;
            tempItem.getComponent(cc.Sprite).spriteFrame =  this.cm_altas.getSpriteFrame(index);
            tempItem.setPosition(cc.p(orgPos.x+HeadWidth/4 + Math.random()*HeadWidth/2,orgPos.y+HeadHeight/4 + Math.random()*HeadHeight/2));
            self.node.addChild(tempItem);
            var move      = cc.moveTo(0.5,cc.p(desPos.x+ Math.random()*160,desPos.y+Math.random()*160));
            move.easing(cc.easeSineInOut());
            var call  = cc.callFunc(function (card_tmp){
               // card_tmp.removeFromParent(true);
            },self,tempItem)
            var sq  = cc.sequence(move,call);
            tempItem.runAction(sq);
            this.coins.push(tempItem);
       // },0.5)
    },
    kanPaiNtf:function(data){
        this.kanlist[data.uin] = 1 ;
    },

    //清桌
    clearTable:function(data){
        this.coins = [] ;
        this.scorenode.active = false ;
        this.kanlist = {} ;
    },

});
